package com.mlz.mlzmall.ui.activity.frag.main;


import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.ProductIntroduceBean;
import com.mlz.mlzmall.bean.ProductshowsBean;
import com.mlz.mlzmall.ui.activity.act.main.GoogDetailsActivity;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.dialog.ReuestDialog;
import com.mlz.mlzmall.ui.model.StringTools;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.ACache;
import com.mlz.mlzmall.ui.utils.util.LogUtils;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;
import com.mlz.mlzmall.volley.volley.NetRequest;
import com.mlz.mlzmall.volley.volley.RequestListener;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Pm on 2016/5/18.
 */
public class GoodsDisplayFragment extends Fragment {
    public List<ProductshowsBean.Data.product> goodsshowlists = new ArrayList<ProductshowsBean.Data.product>();
    private GridView gridView;
    private Context context;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.include_topic_goods_show, container, false);//关联布局文件
        gridView = (GridView) rootView.findViewById(R.id.gv_list_goods_show);
        context = getActivity();
        initData();
        return rootView;
    }

    public void initData() {
        ReuestDialog.showProgressDialog(getActivity());
   //     JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, "http://app.mlzmall.com/api/manufacturer/products?manufacturerId=" + SharedUtils.get(getActivity(), "manufacturerId", 0), null, new Response.Listener<JSONObject>() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.MANUFACTURER.PRODUCTS+"?manufacturerId=" + SharedUtils.get(getActivity(), "manufacturerId", 0), null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                ReuestDialog.dissmissProgressDialog();
                LogUtils.d("获得商品列表信息：" + jsonObject.toString());
                Gson gson = new Gson();
                ProductshowsBean productshowsBean = gson.fromJson(jsonObject.toString(), ProductshowsBean.class);
                goodsshowlists = productshowsBean.getData().getList();
                if (goodsshowlists != null && goodsshowlists.size() > 0) {

                    gridView.setAdapter(new MyAdapter());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("CFintiData", error.getMessage(), error);

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + ACache.get(getActivity()).getAsString("sessionKeyLogin"));
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag("CFintiData");
        // 将请求添加到队列中
        ((MyApplication) getActivity().getApplication()).getRequestQueue().add(jsonRequest);
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return goodsshowlists.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(context).inflate(R.layout.product_detail_item, null);
            ViewHolder vh = new ViewHolder();
            vh.iv_collection_picture = (ImageView) convertView.findViewById(R.id.iv_collection_picture);
            vh.tv_collection_title = (TextView) convertView.findViewById(R.id.tv_collection_title);
            vh.tv_true_prices = (TextView) convertView.findViewById(R.id.tv_true_prices);
            vh.tv_false_prices = (TextView) convertView.findViewById(R.id.tv_false_prices);
            vh.rl_goods = (RelativeLayout) convertView.findViewById(R.id.rl_goods);
            vh.rl_goods.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, GoogDetailsActivity.class);
                    intent.putExtra("productId", goodsshowlists.get(position).getProductId());
                    //       StringTools.shareid=2;
                    context.startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.left_in, R.anim.left_out);
                    //        Toast.makeText(context, goodsshowlists.get(position).getProductId() + "", Toast.LENGTH_SHORT).show();
                }
            });

            if (null != convertView) {
                DecimalFormat df = new DecimalFormat("0.00 ");
                //将图片显示任务添加到执行池，当轮到此ImageView图片将会显示imageView
                ImageLoader.getInstance().displayImage(goodsshowlists.get(position).getImage(), vh.iv_collection_picture);
                vh.tv_collection_title.setText(goodsshowlists.get(position).getName());
                vh.tv_true_prices.setText(df.format(goodsshowlists.get(position).getRefPrice()));
                vh.tv_false_prices.setText(df.format(goodsshowlists.get(position).getPrice()));

            }
            return convertView;
        }

        class ViewHolder {
            ImageView iv_collection_picture;
            TextView tv_collection_title;
            TextView tv_true_prices;
            TextView tv_false_prices;
            RelativeLayout rl_goods;
        }

    }
}